package com.bear.atlassianmcp.oauth.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * OAuth 1.0a 配置属性
 * 支持 Jira 和 Confluence 的 OAuth 认证配置
 */
@Component
@ConfigurationProperties(prefix = "oauth")
public class OAuthProperties {
    
    /**
     * Jira OAuth 配置
     */
    private JiraOAuth jira = new JiraOAuth();
    
    /**
     * Confluence OAuth 配置
     */
    private ConfluenceOAuth confluence = new ConfluenceOAuth();
    
    /**
     * OAuth 通用配置
     */
    private Common common = new Common();
    
    // Getters and Setters
    public JiraOAuth getJira() {
        return jira;
    }
    
    public void setJira(JiraOAuth jira) {
        this.jira = jira;
    }
    
    public ConfluenceOAuth getConfluence() {
        return confluence;
    }
    
    public void setConfluence(ConfluenceOAuth confluence) {
        this.confluence = confluence;
    }
    
    public Common getCommon() {
        return common;
    }
    
    public void setCommon(Common common) {
        this.common = common;
    }
    
    /**
     * Jira OAuth 配置
     */
    public static class JiraOAuth {
        /**
         * Jira 服务器基础 URL
         */
        private String baseUrl;
        
        /**
         * OAuth Consumer Key
         */
        private String consumerKey;
        
        /**
         * OAuth Consumer Secret (对于 RSA-SHA1，这通常是空的)
         */
        private String consumerSecret = "";
        
        /**
         * RSA 私钥路径或内容
         */
        private String privateKey;
        
        /**
         * 回调 URL
         */
        private String callbackUrl = "http://localhost:8081/oauth/jira/callback";
        
        /**
         * 是否启用 OAuth 认证
         */
        private boolean enabled = false;
        
        // Getters and Setters
        public String getBaseUrl() {
            return baseUrl;
        }
        
        public void setBaseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
        }
        
        public String getConsumerKey() {
            return consumerKey;
        }
        
        public void setConsumerKey(String consumerKey) {
            this.consumerKey = consumerKey;
        }
        
        public String getConsumerSecret() {
            return consumerSecret;
        }
        
        public void setConsumerSecret(String consumerSecret) {
            this.consumerSecret = consumerSecret;
        }
        
        public String getPrivateKey() {
            return privateKey;
        }
        
        public void setPrivateKey(String privateKey) {
            this.privateKey = privateKey;
        }
        
        public String getCallbackUrl() {
            return callbackUrl;
        }
        
        public void setCallbackUrl(String callbackUrl) {
            this.callbackUrl = callbackUrl;
        }
        
        public boolean isEnabled() {
            return enabled;
        }
        
        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
        
        /**
         * 获取 Request Token URL
         */
        public String getRequestTokenUrl() {
            return baseUrl + "/plugins/servlet/oauth/request-token";
        }
        
        /**
         * 获取 Authorization URL
         */
        public String getAuthorizationUrl() {
            return baseUrl + "/plugins/servlet/oauth/authorize";
        }
        
        /**
         * 获取 Access Token URL
         */
        public String getAccessTokenUrl() {
            return baseUrl + "/plugins/servlet/oauth/access-token";
        }
    }
    
    /**
     * Confluence OAuth 配置
     */
    public static class ConfluenceOAuth {
        /**
         * Confluence 服务器基础 URL
         */
        private String baseUrl;
        
        /**
         * OAuth Consumer Key
         */
        private String consumerKey;
        
        /**
         * OAuth Consumer Secret (对于 RSA-SHA1，这通常是空的)
         */
        private String consumerSecret = "";
        
        /**
         * RSA 私钥路径或内容
         */
        private String privateKey;
        
        /**
         * 回调 URL
         */
        private String callbackUrl = "http://localhost:8081/oauth/confluence/callback";
        
        /**
         * 是否启用 OAuth 认证
         */
        private boolean enabled = false;
        
        // Getters and Setters
        public String getBaseUrl() {
            return baseUrl;
        }
        
        public void setBaseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
        }
        
        public String getConsumerKey() {
            return consumerKey;
        }
        
        public void setConsumerKey(String consumerKey) {
            this.consumerKey = consumerKey;
        }
        
        public String getConsumerSecret() {
            return consumerSecret;
        }
        
        public void setConsumerSecret(String consumerSecret) {
            this.consumerSecret = consumerSecret;
        }
        
        public String getPrivateKey() {
            return privateKey;
        }
        
        public void setPrivateKey(String privateKey) {
            this.privateKey = privateKey;
        }
        
        public String getCallbackUrl() {
            return callbackUrl;
        }
        
        public void setCallbackUrl(String callbackUrl) {
            this.callbackUrl = callbackUrl;
        }
        
        public boolean isEnabled() {
            return enabled;
        }
        
        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
        
        /**
         * 获取 Request Token URL
         */
        public String getRequestTokenUrl() {
            return baseUrl + "/plugins/servlet/oauth/request-token";
        }
        
        /**
         * 获取 Authorization URL
         */
        public String getAuthorizationUrl() {
            return baseUrl + "/plugins/servlet/oauth/authorize";
        }
        
        /**
         * 获取 Access Token URL
         */
        public String getAccessTokenUrl() {
            return baseUrl + "/plugins/servlet/oauth/access-token";
        }
    }
    
    /**
     * OAuth 通用配置
     */
    public static class Common {
        /**
         * 授权超时时间
         */
        private Duration authorizationTimeout = Duration.ofMinutes(5);
        
        /**
         * 是否自动打开浏览器进行授权
         */
        private boolean autoOpenBrowser = true;
        
        /**
         * Token 存储路径
         */
        private String tokenStorePath = System.getProperty("user.home") + "/.atlassian-mcp/tokens";
        
        // Getters and Setters
        public Duration getAuthorizationTimeout() {
            return authorizationTimeout;
        }
        
        public void setAuthorizationTimeout(Duration authorizationTimeout) {
            this.authorizationTimeout = authorizationTimeout;
        }
        
        public boolean isAutoOpenBrowser() {
            return autoOpenBrowser;
        }
        
        public void setAutoOpenBrowser(boolean autoOpenBrowser) {
            this.autoOpenBrowser = autoOpenBrowser;
        }
        
        public String getTokenStorePath() {
            return tokenStorePath;
        }
        
        public void setTokenStorePath(String tokenStorePath) {
            this.tokenStorePath = tokenStorePath;
        }
    }
}
